﻿<Window x:Class="StackHash.SearchBuilder"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vcs="clr-namespace:StackHash.ValueConverters"
    Title="Search Builder - StackHash" Height="400" MinHeight="400" Width="675" MinWidth="675" Icon="/StackHash;component/StackHash.ico" ShowInTaskbar="False" WindowStartupLocation="CenterOwner" Closed="Window_Closed">
    <Window.Resources>
        <ResourceDictionary>
            
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MasterResources.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <DataTemplate x:Key="fieldComboTemplate">
                <ComboBox SelectedItem="{Binding Path=FieldInfo, Mode=TwoWay}" ItemsSource="{Binding AvailableFields}" SelectionChanged="ComboBox_SelectionChanged">
                    <ComboBox.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" />
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource {x:Static SystemColors.ControlColorKey}}" />
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{DynamicResource {x:Static SystemColors.HighlightTextColorKey}}" />
                    </ComboBox.Resources>
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DisplayName}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </DataTemplate>

            <DataTemplate x:Key="optionsComboTemplate">
                <ComboBox SelectedItem="{Binding Path=OptionInfo, Mode=TwoWay}" ItemsSource="{Binding AvailableOptions}" SelectionChanged="ComboBox_SelectionChanged">
                    <ComboBox.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" />
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource {x:Static SystemColors.ControlColorKey}}" />
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{DynamicResource {x:Static SystemColors.HighlightTextColorKey}}" />
                    </ComboBox.Resources>
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DisplayName}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </DataTemplate>

            <DataTemplate x:Key="searchTemplate">
                <TextBox Text="{Binding Path=Search, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" TextChanged="TextBox_TextChanged" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
            </DataTemplate>

            <DataTemplate x:Key="fieldButtonsTemplate">
                <StackPanel Orientation="Horizontal">
                    <Button Style="{StaticResource ToolBarButtonStyle}" Margin="0,0,7,0" Name="buttonAddField" Click="buttonAddField_Click" Tag="{Binding ElementName=listOptions, Path=ItemsSource}">
                        <Image Source="Help\add-field.png" Style="{StaticResource ButtonImageStyle}" />
                    </Button>
                    <Button Style="{StaticResource ToolBarButtonStyle}" Name="buttonDeleteField" Click="buttonDeleteField_Click" Tag="{Binding ElementName=listOptions, Path=ItemsSource}" IsEnabled="{Binding ElementName=listOptions, Path=ItemsSource, Converter={StaticResource moreThanOneConverter}}">
                        <Image Source="Help\remove-field.png" Style="{StaticResource ButtonImageStyle}" />
                    </Button>
                </StackPanel>
            </DataTemplate>
            
        </ResourceDictionary>
    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding Command="Help" x:Name="commandBindingHelp" Executed="commandBindingHelp_Executed" />
    </Window.CommandBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="10" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        
        <Label Margin="7" Target="{Binding ElementName=listCriteria}">Search _Criteria:</Label>
        <ListBox Grid.Column="1" Margin="0,10,10,0" Name="listCriteria">
            <ListBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="3">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />                            
                        </Grid.ColumnDefinitions>
                        <ListView ItemsSource="{Binding Options}" Name="listOptions" SelectionMode="Single">
                            <ListView.ItemContainerStyle>
                                <Style TargetType="ListViewItem">
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                </Style>
                            </ListView.ItemContainerStyle>
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn Width="125" Header="Field" CellTemplate="{StaticResource fieldComboTemplate}" />
                                    <GridViewColumn Width="125" Header="Search Type" CellTemplate="{StaticResource optionsComboTemplate}" />
                                    <GridViewColumn Width="150" Header="Search" CellTemplate="{StaticResource searchTemplate}" />
                                    <GridViewColumn Width="70" CellTemplate="{StaticResource fieldButtonsTemplate}" />
                                </GridView>
                            </ListView.View>
                        </ListView>
                        <TextBlock Grid.Row="1" FontWeight="Bold" VerticalAlignment="Center">OR</TextBlock>
                        <Button Grid.Column="1" Style="{StaticResource ToolBarButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="7,0,0,0" Name="buttonDeleteCriteria" Click="buttonDeleteCriteria_Click" IsEnabled="{Binding ElementName=listCriteria, Path=ItemsSource, Converter={StaticResource moreThanOneConverter}}">
                            <Image Source="Help\remove-criteria.png" Style="{StaticResource ButtonImageStyle}" />
                        </Button>
                        <Button Grid.Row="1" Grid.Column="1" Style="{StaticResource ToolBarButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="7,0,0,0" Name="buttonAddCriteria" Click="buttonAddCriteria_Click">
                            <Image Source="Help\add-criteria.png" Style="{StaticResource ButtonImageStyle}" />
                        </Button>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        
        <Label Margin="7,7,7,0" Grid.Row="1">Search Text:</Label>
        <TextBlock Margin="0,10,10,0" Grid.Row="1" Grid.Column="2" Padding="3" TextWrapping="Wrap" Background="LightYellow" Name="textBlockSearchString" />

        <Label Margin="7,7,7,0" Grid.Row="2" Target="{Binding ElementName=textBoxSearchName}">_Search Name:</Label>
        <TextBox Grid.Row="2" Grid.Column="1" Margin="0,10,10,0" Name="textBoxSearchName" TextChanged="textBoxSearchName_TextChanged" Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />

        <Border Grid.Row="4" Grid.ColumnSpan="2" Style="{StaticResource ButtonBorder}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                
                <Button Margin="10" Command="Help">_Help</Button>

                <StackPanel Margin="7" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
                    <Button Margin="3" Name="buttonOK" IsDefault="True" Click="buttonOK_Click">OK</Button>
                    <Button Margin="3" IsCancel="True">Cancel</Button>
                </StackPanel>
            </Grid>
        </Border>
    </Grid>
</Window>
